﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CaclLib.Handle
{
    public class ExcelHelper
    {
        /// <summary>
        /// 读取Excel文件数据到DataTable
        /// </summary>
        /// <param name="filePath">Excel文件路径</param>
        public static DataTable Import_Excel(string filePath)
        {
            string sqlconn = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";

            string sql = @"select * from [Sheet1$]";
            DataTable dt = new DataTable();
            try
            {
                using (OleDbConnection conn = new OleDbConnection(sqlconn))
                {
                    using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn))
                    {
                        adapter.Fill(dt);

                        //this.LoadDataGridView(dt);
                    }
                }
                return dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show("打开文件出错，错误信息：" + ex.Message.ToString(), "提示");
                return dt;
            }
        }
    }
}
